﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Collections.Generic;
using Model;

public partial class PCBA_PCBAssemble_PCBAssembleAdd : System.Web.UI.Page
{
    private PCBAssemble pam;
    private PCBAssembleDetail pad;
    private DataRow dr;
    private DataTable pamdt
    {
        get
        {
            return (DataTable)ViewState["pamdt"];
        }
        set
        {
            ViewState["pamdt"] = value;
        }
    }

    public PCBA_PCBAssemble_PCBAssembleAdd()
    {
        pam = new PCBAssemble();
        pad = new PCBAssembleDetail();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            pamdt = new DataTable();
            pamdt.Columns.Add(new DataColumn("BatchID", Type.GetType("System.String")));
            pamdt.Columns.Add(new DataColumn("StuffTypeID", Type.GetType("System.String")));
            pamdt.Columns.Add(new DataColumn("VendorSN", Type.GetType("System.String")));
        }
        //if (User.Identity.Name == "10307")
        //{
        //    this.Button3.Visible = true;
        //}
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        string batchid = this.TextBox2.Text.Trim();
        try
        {
            this.TextBox3.Text = batchid.Substring(batchid.Length - 17, 10);
            this.TextBox4.Focus();
        }
        catch 
        {
            string str = "批次序列号不能少于19位！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        dr = pamdt.NewRow();
        dr["BatchID"] = this.TextBox2.Text.Trim();
        dr["StuffTypeID"] = this.TextBox3.Text.Trim();
        dr["VendorSN"] = this.TextBox4.Text.Trim();
        if (pamdt.Select("BatchID = '" + this.TextBox2.Text.Trim() + "' and VendorSN ='" + this.TextBox4.Text.Trim() + "'").Length == 0)
        {
            pamdt.Rows.InsertAt(dr, 0);
            Bindpam();
            this.TextBox2.Text = "";
            this.TextBox3.Text = "";
            this.TextBox4.Text = "";
            this.TextBox2.Focus();
        }
        else
        {
            string str = "已经在列表中";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }

    protected void Bindpam()
    {
        this.GridView1.DataSource = pamdt;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        pamdt.Select("BatchID = '" + GridView1.Rows[e.RowIndex].Cells[0].Text + "' and VendorSN='" + GridView1.Rows[e.RowIndex].Cells[2].Text + "'")[0].Delete();
        Bindpam();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (pamdt.Rows.Count < 1)
        {
            string str = "请先添加关键物料信息!";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
        else 
        {
            string pCBASN = this.TextBox1.Text.Trim();

            PCBAssembleInfo padi1 = pam.LoadEntity(pCBASN);
            if (padi1.PCBASN == pCBASN)
            {
                string str = "此PCBA序列号已经装配过，无法重复装配!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
            else
            {
                string pCBTypeID = this.TextBox5.Text.Trim();
                PCBABuildandAdd(pCBASN, pCBTypeID);

                DateTime dt = DateTime.Now;
                string ip = IPGet.getIp();
                PCBAssembleInfo padi = new PCBAssembleInfo(pCBASN, dt, User.Identity.Name, ip,"Auto");
                if (pam.PCBAssembleDetailItem_Insert(padi, pamdt))
                {
                    string newpCBASN = pCBASN.Substring(0, pCBASN.Length - 4) + (int.Parse(pCBASN.Substring(pCBASN.Length - 4)) + 1).ToString("D4");
                    this.TextBox1.Text = newpCBASN;
                    this.TextBox1.Focus();
                    string str = pCBASN + " 提交成功!";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                }
                else
                {
                    string str = "提交失败，请联系相关人员!";
                    ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
                }
            }
        }        
    }

    protected void PCBABuildandAdd(string pCBASN, string pCBTypeID)
    {
        PCBAInfo pi = new PCBAInfo(pCBASN, pCBTypeID);
        PCBA p = new PCBA();
        p.Insert(pi);
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string ip = IPGet.getIp();
        string pCBTypeID = this.TextBox5.Text.Trim();
        IList<PCBAssembleDetailInfo> list = pad.GetPCBAssembleDetailbyIPPID(pCBTypeID, ip);
        Appendtoamddt(list);
    }

    protected void Appendtoamddt(IList<PCBAssembleDetailInfo> list)
    {
        pamdt.Clear();
        foreach (PCBAssembleDetailInfo padi in list)
        {
            dr = pamdt.NewRow();
            dr["BatchID"] = padi.BatchID;
            dr["StuffTypeID"] = padi.StuffTypeID;
            dr["VendorSN"] = padi.VendorSN;
            pamdt.Rows.Add(dr);
        }
        Bindpam();
    }
}
